package com.ruoyi.qiushi.service.mall.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qiushi.domain.mall.TradeAfterSale;
import com.ruoyi.qiushi.domain.mall.TradeOrder;
import com.ruoyi.qiushi.domain.mall.TradeOrderItem;
import com.ruoyi.qiushi.domain.mall.TradeServiceOrder;
import com.ruoyi.qiushi.mapper.mall.TradeAfterSaleMapper;
import com.ruoyi.qiushi.mapper.mall.TradeOrderItemMapper;
import com.ruoyi.qiushi.mapper.mall.TradeOrderMapper;
import com.ruoyi.qiushi.mapper.mall.TradeServiceOrderMapper;
import com.ruoyi.qiushi.mapper.user.MemberUserMapper;
import com.ruoyi.qiushi.service.mall.ITradeAfterSaleService;
import com.ruoyi.qiushi.service.wechat.WechatPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 售后订单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@Service
public class TradeAfterSaleServiceImpl implements ITradeAfterSaleService {
    @Autowired
    private TradeAfterSaleMapper tradeAfterSaleMapper;
    @Autowired
    private TradeOrderItemMapper tradeOrderItemMapper;
    @Autowired
    private TradeServiceOrderMapper tradeServiceOrderMapper;
    @Autowired
    private TradeOrderMapper tradeOrderMapper;
    @Autowired
    private WechatPay wechatPay;
    @Autowired
    private MemberUserMapper memberUserMapper;

    /**
     * 查询售后订单
     *
     * @param id 售后订单主键
     * @return 售后订单
     */
    @Override
    public TradeAfterSale selectTradeAfterSaleById(Long id) {
        return tradeAfterSaleMapper.selectTradeAfterSaleById(id);
    }

    /**
     * 查询售后订单列表
     *
     * @param tradeAfterSale 售后订单
     * @return 售后订单
     */
    @Override
    public List<TradeAfterSale> selectTradeAfterSaleList(TradeAfterSale tradeAfterSale) {
        List<TradeAfterSale> list = tradeAfterSaleMapper.selectTradeAfterSaleList(tradeAfterSale);

        for (int i = 0; i < list.size(); i++) {
            TradeAfterSale temp = list.get(i);
            String userName = memberUserMapper.selectMemberUserById(temp.getUserId()).getUserName();
            temp.setUserName(userName);

            list.set(i, temp);
        }

        return list;
    }

    /**
     * 新增售后订单
     *
     * @param tradeAfterSale 售后订单
     * @return 结果
     */
    @Override
    public int insertTradeAfterSale(TradeAfterSale tradeAfterSale) {
        tradeAfterSale.setCreateTime(DateUtils.getNowDate());
        return tradeAfterSaleMapper.insertTradeAfterSale(tradeAfterSale);
    }

    /**
     * 修改售后订单
     *
     * @param tradeAfterSale 售后订单
     * @return 结果
     */
    @Override
    public int updateTradeAfterSale(TradeAfterSale tradeAfterSale) {
        TradeAfterSale temp = tradeAfterSaleMapper.selectTradeAfterSaleById(tradeAfterSale.getId()); // 当前状态

        LoginUser loginUser = SecurityUtils.getLoginUser();
        tradeAfterSale.setAuditUserId(loginUser.getUserId());
        tradeAfterSale.setAuditUserName(loginUser.getUsername());

        if (temp.getStatus().equals("0")) {
            tradeAfterSale.setAuditTime(new Date());
            if (tradeAfterSale.getBullst().equals("0")) { // 同意 不退款
                tradeAfterSale.setStatus("2"); // 状态改为成功

                if (tradeAfterSale.getSpuType().equals("2")) {
                    TradeOrderItem tradeOrderItem = tradeOrderItemMapper.selectTradeOrderItemById(tradeAfterSale.getOrderItemId());
                    tradeOrderItem.setAfterSaleStatus("2");
                    tradeOrderItem.setUpdateTime(new Date());

                    tradeOrderItemMapper.updateTradeOrderItem(tradeOrderItem);
                } else if(tradeAfterSale.getSpuType().equals("3")) {
                    TradeServiceOrder tradeServiceOrder = tradeServiceOrderMapper.selectTradeServiceOrderById(tradeAfterSale.getOrderId());
                    tradeServiceOrder.setAfterSaleStatus("2");;
                    tradeServiceOrder.setUpdateTime(new Date());

                    tradeServiceOrderMapper.updateTradeServiceOrder(tradeServiceOrder);
                }
                tradeAfterSale.setRefundTime(new Date());
            } else if (tradeAfterSale.getBullst().equals("1")) { // 同意 退款
                tradeAfterSale.setStatus("1"); // 状态改为退款中

                String outRefundNo = String.valueOf(UUID.randomUUID());//退款单号 系统生成
                tradeAfterSale.setRefundTradeNo(outRefundNo); // 设置退款单号

                if (tradeAfterSale.getSpuType().equals("2")) {
                    TradeOrder tradeOrder = tradeOrderMapper.selectTradeOrderById(tradeAfterSale.getOrderId());

                    BigDecimal refund_price = tradeAfterSale.getRefundPrice().multiply(new BigDecimal(100));
                    BigDecimal actual_price = tradeOrder.getActualPrice().multiply(new BigDecimal(100));

                    wechatPay.refund(outRefundNo,
                            tradeOrder.getNo(),
                            refund_price.longValue(),
                            actual_price.longValue());

                } else if (tradeAfterSale.getSpuType().equals("3")) {
                    TradeServiceOrder tradeServiceOrder = tradeServiceOrderMapper.selectTradeServiceOrderById(tradeAfterSale.getOrderId());

                    BigDecimal refund_price = tradeAfterSale.getRefundPrice().multiply(new BigDecimal(100));
                    BigDecimal actual_price = tradeServiceOrder.getActualPrice().multiply(new BigDecimal(100));

                    wechatPay.refund(outRefundNo,
                            tradeServiceOrder.getNo(),
                            refund_price.longValue(),
                            actual_price.longValue());

                }

            } else if (tradeAfterSale.getBullst().equals("2")) { // 拒绝 不退款
                tradeAfterSale.setStatus("3"); // 状态改为拒绝

                if (tradeAfterSale.getSpuType().equals("2")) {
                    TradeOrderItem tradeOrderItem = tradeOrderItemMapper.selectTradeOrderItemById(tradeAfterSale.getOrderItemId());
                    tradeOrderItem.setAfterSaleStatus("2");
                    tradeOrderItem.setUpdateTime(new Date());

                    tradeOrderItemMapper.updateTradeOrderItem(tradeOrderItem);
                } else if(tradeAfterSale.getSpuType().equals("3")) {
                    TradeServiceOrder tradeServiceOrder = tradeServiceOrderMapper.selectTradeServiceOrderById(tradeAfterSale.getOrderId());
                    tradeServiceOrder.setAfterSaleStatus("2");;
                    tradeServiceOrder.setUpdateTime(new Date());

                    tradeServiceOrderMapper.updateTradeServiceOrder(tradeServiceOrder);
                }
            }
        }

        tradeAfterSale.setUpdateTime(new Date());
        return tradeAfterSaleMapper.updateTradeAfterSale(tradeAfterSale);
    }

    /**
     * 删除售后订单信息
     *
     * @param id 售后订单主键
     * @return 结果
     */
    @Override
    public int deleteTradeAfterSaleById(Long id) {
        return tradeAfterSaleMapper.deleteTradeAfterSaleById(id);
    }
}
